<template>
  <div>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道:{{show?"点击移除频道":"点击进入频道"}}</span>
        <span @click="show=!show" >{{show?"完成":"编辑"}}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="obj in userchannelList" :key="obj.id"
          @click="channelcheck(obj)"
        >
          <div class="channel-item van-hairline--surround">
            {{obj.name}}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-show="show&&obj.id!==0"

            >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                  @click="delFn(obj)"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj in unchannel" :key="obj.id" @click="addFn(obj)">
          <div class="channel-item van-hairline--surround" >
            {{obj.name}}
          </div>
        </van-col>
      </van-row>
    </div>

  </div>

</template>

<script>
import { updateChannelAPI } from '@/api'
export default {
  props: {
    userchannelList: { // 用户选定的频道列表  {id和name}
      type: Array
    },
    allChannel: {
      type: Array
    }
  },
  computed: {
    unchannel () { // 更多频道未选中的
      return this.allChannel.filter((item) => {
        const index = this.userchannelList.findIndex(obj => obj.id === item.id)
        if (index !== -1) {
          return false
        } else {
          return true
        }
      })
    }
  },
  data () {
    return {
      show: false
    }
  },
  methods: {
    // 添加已选频道
    addFn (obj) {
      // 添加用户选中的频道到数组---这个是在数组中添加
      this.userchannelList.push(obj)
      this.updatelist()
    },
    // 删除已选频道
    delFn (item) {
      const index1 = this.userchannelList.findIndex((obj) => {
        return obj.id === item.id
      })
      this.userchannelList.splice(index1, 1)
      this.updatelist()
    },
    // 向后台传数据,重新渲染页面
    async updatelist () {
      //   同步给后台
      // 将推荐排成为新数组
      const arr = []
      //   将新数组中每个对象进行处理 成为新数组
      this.userchannelList.forEach((obj) => {
        const newobj = { ...obj }
        arr.push(newobj)
      })
      //  把推荐剔除
      const index = arr.findIndex((obj) => obj.id === 0)
      arr.splice(index, 1)
      // 构建一个给后台的新数组
      arr.forEach((obj, index) => {
        delete obj.name
        obj.seq = index + 1
      })
      // 调接口
      await updateChannelAPI({ channels: arr })
    },
    channelcheck (obj) {
      if (!this.show) {
        this.$emit('channelid', obj)
      }
    }
  }

}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small_title {
  font-size: 12px;
  color: gray;
}
</style>
